package com.atguigu.web;

import com.atguigu.pojo.Book;
import com.atguigu.pojo.Page;
import com.atguigu.service.BookService;
import com.atguigu.service.Impl.BookServiceImpl;
import com.atguigu.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName BookServlet
 * @Description
 * @Author 周轩霆
 * @Date 2022/4/17 12:59
 **/

public class BookServlet extends BaseServlet {
    private BookService bookService = new BookServiceImpl();


    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        //封装，将数据保存的数据库
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 0);
        pageNo++;
        Book book = WebUtils.copyParamToBean(req.getParameterMap(), new Book());
        bookService.addBook(book);
        //重定向
        resp.sendRedirect("http://localhost:8080/book/manager/bookServlet?action=page&pageNo=" + pageNo);
        // req.getRequestDispatcher("/manager/bookServlet?action=list").forward(req, resp);

    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = WebUtils.parseInt(req.getParameter("id"), 0);//0是默认值
        bookService.deleteBookById(id);
        //重定向
        resp.sendRedirect("http://localhost:8080/book/manager/bookServlet?action=page&pageNo="+req.getParameter("pageNo"));
    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        //封装，将数据保存的数据库

        Book book = WebUtils.copyParamToBean(req.getParameterMap(), new Book());
        bookService.updateBook(book);
        //  page(req,resp);
        //重定向
        resp.sendRedirect("http://localhost:8080/book/manager/bookServlet?action=page&pageNo="+req.getParameter("pageNo"));
        // req.getRequestDispatcher("/manager/bookServlet?action=list").forward(req, resp);

    }

    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //通过BookService查询全部图书
        List<Book> books = bookService.queryBooks();
        //把全部图书保存到Request域中
        req.setAttribute("books", books);
        //请求转发
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req, resp);


    }

    protected void getBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取要修改图书的id
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        Book book = bookService.queryBookById(id);
        //查询到相关的数据后，保存到域对象中。
        req.setAttribute("book", book);
        //请求转发
        req.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(req, resp);


    }


    /**
     * @return void
     * @Author 周轩霆
     * @Description 处理分页功能
     * @Date 14:12 2022/4/18
     * @Param [req, resp]
     */
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求的参数 pageNo,pageSize
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //2.调用bookService.page(pageNo,pageSize)方法:page对象
        Page<Book> page = bookService.page(pageNo, pageSize);
        page.setUrl("manager/bookServlet?action=page");
        //3.保存page对象到Request域中
        req.setAttribute("page", page);
        //4.请求转发到pages/manager/book_manager.jsp
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req, resp);

    }
}
